package cn.edu.zut.DAO;

import cn.edu.zut.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository(value="bdi1")
public class StudentDaoImp1 implements StudentDao {
    private JdbcTemplate jdbcTemplate;
    private List<Student>students;
    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
        this.jdbcTemplate=jdbcTemplate;
    }
    @Autowired
    public void setStudent(List<Student>students){
        this.students=students;
    }
    @Override
    public void insertStudent(Student student) {
        String INSERT_STUDENT="INSERT INTO student(Sno,name,sex,grade) value(?,?,?,?)";
        Object[]au={student.getSno(),student.getName(),student.getSex(),student.getGrade()};
        jdbcTemplate.update(INSERT_STUDENT,au);
    }
    @Override
    public List<Student> queryAll() {
        String QUERY_ALL_STUDENT="select * from student";
        RowMapper<Student> rowMapper=new BeanPropertyRowMapper<>(Student.class);
        students=jdbcTemplate.query(QUERY_ALL_STUDENT,rowMapper);
        return students;
    }
}
